package team.seekers.oj.codetest.dao;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Repository;
import team.seekers.oj.codetest.entity.dao.CodeTestDO;
import team.seekers.oj.codetest.mapper.CodeTestMapper;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

@Repository
public class CodeTestDao extends ServiceImpl<CodeTestMapper, CodeTestDO> {

    public  List<Long> codeTestProblemsToProblemIdList(byte[] problems) {
        if (problems == null) {
            return Lists.newArrayList();
        }
        ByteBuffer wrap = ByteBuffer.wrap(problems);
        List<Long> codeTestProblemIdList = new ArrayList<>(problems.length / 8);
        for (int i = 0, n = problems.length; i < n; i += 8) {
            codeTestProblemIdList.add(wrap.getLong(i));
        }
        return codeTestProblemIdList;
    }

    public  byte[] codeTestProblemsFromProblemIdList(List<Long> codeTestProblemIdList) {
        ByteBuf byteBuf = Unpooled.buffer(codeTestProblemIdList.size() * 8);
        codeTestProblemIdList.forEach(byteBuf::writeLong);
        return byteBuf.array();
    }
}
